# 使用官方Python基础镜像
FROM python:3.12-slim

# 设置工作目录
WORKDIR /app

# 清理默认源配置
RUN rm -rf /etc/apt/sources.list.d/*

# 写入清华源列表（适配 bookworm）
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free\ndeb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free\ndeb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free\ndeb http://mirrors.tuna.tsinghua.edu.cn/debian-security/ bookworm-security main contrib non-free" > /etc/apt/sources.list

# 安装系统依赖并清理缓存
# 出现build问题时尝试运行以下指令，用完记得删除
# apt-get clean \
# apt-get update \
RUN apt-get install -y --no-install-recommends \
    gcc \
    python3-dev \
    libpq-dev \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# 复制 requirements.txt 并安装 Python 依赖
COPY requirements.txt .
# RUN pip install --no-cache-dir -r requirements.txt --index-url https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install -r requirements.txt --index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 验证 psycopg2 是否能被正确导入
RUN python -c "import psycopg2; print(psycopg2.__version__)"

# 复制应用程序代码
COPY . .

#修改时区
RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone

# 暴露端口
EXPOSE 8000

# 生产环境运行命令
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]